package com.tanhua.controller;

import com.tanhua.service.CommentsService;
import com.tanhua.service.MovementService;
import model.mongo.Movement;
import model.vo.MovementsVo;
import model.vo.PageResult;
import model.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentsService commentsService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity movements(
            Movement movement,
            MultipartFile imageContent[]) throws IOException {
        movementService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/all")
    public ResponseEntity findByUserId(
            Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping
    public ResponseEntity movements(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/recommend")
    public ResponseEntity recommend(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询单条动态
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementService.findById(movementId);
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.likeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.dislikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer loveCount = commentsService.loveComment(movementId);
        return ResponseEntity.ok(loveCount);
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer loveCount = commentsService.unloveComment(movementId);
        return ResponseEntity.ok(loveCount);
    }

    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList() {
        List<VisitorsVo> visitorsVoList = movementService.queryVisitorsList();
        return ResponseEntity.ok(visitorsVoList);
    }
}